package com.chookss.home.publish;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.chookss.video.VideoNewCatesActivity;
import com.chookss.video.entity.VideoEntity;
import com.lvgroup.hospital.base.BaseAct;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditVedioActivity extends BaseAct {


    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.common_right_txt)
    TextView commonRightTxt;
    @BindView(R.id.edTitle)
    EditText edTitle;
    @BindView(R.id.ivVedio)
    ImageView ivVedio;
    @BindView(R.id.edKey)
    EditText edKey;
    @BindView(R.id.tvDirectory)
    TextView tvDirectory;
    private String directoryNames, directoryCodes;
    private VideoEntity videoEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_vedio);
        ButterKnife.bind(this);
        initState();
    }

    @Override
    public void initData() {

        commonTitleTxt.setText("发布");
        commonRightTxt.setText("提交");
        commonRightTxt.setVisibility(View.VISIBLE);
        commonRightTxt.setTextColor(getResources().getColor(R.color.main_color));
        videoEntity = (VideoEntity) getIntent().getSerializableExtra("video");
        edTitle.setText(videoEntity.videoTitle);
        edKey.setText(videoEntity.videoKeyWord);
        directoryNames = videoEntity.catalogNames;
        directoryCodes = videoEntity.catalogCodes;
        if(directoryNames.contains("我的上传,")){
            tvDirectory.setText(directoryNames.replace("我的上传,",""));
        }else if(directoryNames.contains(",我的上传")){
            tvDirectory.setText(directoryNames.replace(",我的上传",""));
        }else{
            tvDirectory.setText(directoryNames);
        }

        GlideUtils.loadCustomRound(this, videoEntity.videoCoverPath, ivVedio, 4);
    }

    private void editData() {
        Map<String, String> map = new HashMap<>();
        map.put("id", videoEntity.id);
        map.put("videoTitle", edTitle.getText().toString());
        map.put("videoId", videoEntity.videoId);
        map.put("videoKeyWord", edKey.getText().toString());
        map.put("videoCoverPath", videoEntity.videoCoverPath);
        if (Utils.isNull(videoEntity.videoCollectionsCode)) {
            map.put("videoCollectionsCode", "");
            map.put("videoCollectionsName", "");
        } else {
            map.put("videoCollectionsCode", videoEntity.videoCollectionsCode);
            map.put("videoCollectionsName", videoEntity.videoCollectionsName);
        }
        map.put("videoOpenStatus", videoEntity.videoOpenStatus);
        map.put("videoAuditStatus", "0");
        map.put("catalogCodes", directoryCodes);
        map.put("videoLocation", videoEntity.videoLocation);
        MyHttpRequest.postRequest(Urls.updateAppVideoById, this, SignUtils2.makeSignStr(map), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    dismissLoading();
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        EventBus.getDefault().post(new MyEvent("RefreshWork_Video"));
                        MyToast.show("视频修改成功");
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    MyToast.show("视频修改失败，请稍后重试");
                }
                finish();
            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MyEvent messageEvent) {
        //事件订阅者处理事件
        if (messageEvent.getMap() != null) {
            Map<String, String> map = messageEvent.getMap();
            if (map.containsKey("videoCate") && map.containsKey("videoNames")) {
                if (tvDirectory == null) {
                    return;
                }
                directoryNames = map.get("videoNames");
                directoryCodes = map.get("videoCate");
                tvDirectory.setText(directoryNames);
            }
        } else {
            String msg = messageEvent.getMessage();
            if (Utils.isNull(msg)) {
                return;
            }
        }
    }


    @OnClick({R.id.common_title_back, R.id.common_right_txt, R.id.llDirectory})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.common_right_txt:
                if (Urls.antiShake.check()) {
                    return;
                }
                if (edTitle == null) {
                    return;
                }
                if (Utils.isNull(edTitle.getText().toString().replace(" ", ""))) {
                    MyToast.show("请输入标题");
                    return;
                }
                if (Utils.isNull(tvDirectory.getText().toString())) {
                    MyToast.show("请选择所属目录");
                    return;
                }
                showLoading();
                editData();
                break;
            case R.id.llDirectory:
                Intent intent = new Intent(this, VideoNewCatesActivity.class);
                intent.putExtra("videoType", "3");
                startActivity(intent);
                break;
        }
    }
}
